package pl.bank.view.beans;

import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.validator.ValidatorException;

import oracle.adf.share.logging.ADFLogger;
import oracle.adf.view.rich.component.rich.layout.RichPanelFormLayout;
import oracle.adf.view.rich.component.rich.nav.RichCommandButton;

import oracle.binding.AttributeBinding;
import oracle.binding.OperationBinding;

import oracle.jbo.Row;
import oracle.jbo.domain.Number;
import oracle.jbo.domain.Timestamp;

import pl.bank.model.view.DefinedrecipientsViewRowImpl;

import utils.ADFUtils;


public class TransactionBean {
    private ADFLogger LOG = ADFLogger.createADFLogger(this.getClass().getName());
    private RichPanelFormLayout transactionForm;
    private RichCommandButton button;

    public TransactionBean() {
        super();
    }

    public void createTransaction(ActionEvent actionEvent) {
        LOG.entering("TransactionBean", "createTransaction");
        RichCommandButton buttonSend = (RichCommandButton)actionEvent.getComponent();
        buttonSend.setDisabled(true);
        AttributeBinding attr = ADFUtils.findControlBinding("TrCreationDateIn");
        attr.setInputValue(new Timestamp(new Date()));

        ADFUtils.resetEditableValuesInComponent(transactionForm);
        transactionForm.setVisible(true);


        LOG.exiting("TransactionBean", "createTransaction");
    }

    public void amountValidator(FacesContext facesContext, UIComponent uiComponent, Object object) {

        Number value = (Number)object;

        AttributeBinding attr = ADFUtils.findControlBinding("AcAvailableAmount");
        Number amount = (Number)attr.getInputValue();

        if (value.doubleValue() > amount.doubleValue()) {
            FacesMessage fm = new FacesMessage();
            fm.setSummary("Nie masz tyle pieniędzy");
            fm.setSeverity(FacesMessage.SEVERITY_ERROR);

            throw new ValidatorException(fm);
        }


    }

    public void sendTransaction(ActionEvent actionEvent) {
        transactionForm.setVisible(false);
        button.setDisabled(false);

        AttributeBinding attr = ADFUtils.findControlBinding("AcAvailableAmount");
        Number amount = (Number)attr.getInputValue();

        AttributeBinding attrStart = ADFUtils.findControlBinding("TrStartAcAmountIn");
        attrStart.setInputValue(amount);

        AttributeBinding attrIn = ADFUtils.findControlBinding("TrAmountIn");
        Number amountIn = (Number)attrIn.getInputValue();
        attr.setInputValue(amount.subtract(amountIn));

        Number ZERO = new Number(0);
        attrIn.setInputValue(ZERO.sub(amountIn));

        OperationBinding create = ADFUtils.findOperation("Createwithparameters");
        create.execute();

        cleanInputs();

    }

    public void setTransactionForm(RichPanelFormLayout transactionForm) {
        this.transactionForm = transactionForm;
    }

    public RichPanelFormLayout getTransactionForm() {
        return transactionForm;
    }

    public void rollbackTransaction(ActionEvent actionEvent) {
        transactionForm.setVisible(false);
        button.setDisabled(false);
        cleanInputs();
    }

    public void setButton(RichCommandButton button) {
        this.button = button;
    }

    public RichCommandButton getButton() {
        return button;
    }

    public void dataForSelectedRecipient(ActionEvent actionEvent) {
        AttributeBinding attr = ADFUtils.findControlBinding("DrSelectedIn");
        Number keyId = (Number)attr.getInputValue();
        OperationBinding find = ADFUtils.findOperation("findRecipientsRow");
        find.getParamsMap().put("key", keyId);
        Row row = (Row)find.execute();
        if (row != null) {
            DefinedrecipientsViewRowImpl rRow = (DefinedrecipientsViewRowImpl)row;
            attr = ADFUtils.findControlBinding("TrAcNumerReciverIn");
            attr.setInputValue(rRow.getDrAcNumber());
            attr = ADFUtils.findControlBinding("TrRecivierIn");
            attr.setInputValue(rRow.getDrRecivier());
            attr = ADFUtils.findControlBinding("TrTitleIn");
            attr.setInputValue(rRow.getDrTitle());
            ADFUtils.resetEditableValuesInComponent(transactionForm);
        } else {
            cleanInputs();
        }
    }


    private void cleanInputs() {
        AttributeBinding attr = ADFUtils.findControlBinding("TrAmountIn");
        attr.setInputValue(null);
        attr = ADFUtils.findControlBinding("TrCreationDateIn");
        attr.setInputValue(null);
        attr = ADFUtils.findControlBinding("TrAcNumerReciverIn");
        attr.setInputValue(null);
        attr = ADFUtils.findControlBinding("TrRecivierIn");
        attr.setInputValue(null);
        attr = ADFUtils.findControlBinding("TrTitleIn");
        attr.setInputValue(null);

        ADFUtils.resetEditableValuesInComponent(transactionForm);
    }
}
